package api

import (
	"log"

	"gitee.com/dingiyan/goMyAdmin/db"
	"gitee.com/dingiyan/goMyAdmin/service/dialect"
	"gitee.com/dingiyan/goMyAdmin/service/interfaces"
	"github.com/gin-gonic/gin"
)

// 获取一个数据库的表列表
func GetTableList(c *gin.Context) {
	var body struct {
		DsId         uint   `json:"dsId"`
		DatabaseName string `json:"databaseName"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		log.Println(err)
		c.JSON(200, resError(1, "参数错误！"))
		return
	}

	serverConfig, err := db.GetServerById(body.DsId)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "查询错误！"))
		return
	}
	dbm := dialect.GetDialect(dialect.DialectMysql)

	config := serverToDatasourcesConfig(serverConfig, body.DatabaseName)

	_, err1 := dbm.ConnectDatasources(&config)
	if err1 != nil {
		log.Println(err1)
		c.JSON(200, resError(2, "连接错误！"))
		return
	}

	tables, err := dbm.GetTables()
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(3, "查询错误！"))
	}
	if tables == nil {

		tables = []interfaces.TableDetail{}
	}
	c.JSON(200, success(gin.H{
		"tables": tables,
	}))
}

// 创建一个表，表及字段
func CreateTable(c *gin.Context) {
	var body struct {
		DsId         uint                      `json:"dsId"`
		DatabaseName string                    `json:"databaseName"`
		TableName    string                    `json:"tableName"`
		Fields       []interfaces.FieldOptions `json:"fields"`
		TableArgs    map[string]any            `json:"tableArgs"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		log.Println(err)
		c.JSON(200, resError(1, "参数错误！"))
		return
	}
	serverConfig, err := db.GetServerById(body.DsId)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "查询错误！"))
		return
	}
	dbm := dialect.GetDialect(dialect.DialectMysql)
	config := serverToDatasourcesConfig(serverConfig, body.DatabaseName)
	_, err1 := dbm.ConnectDatasources(&config)
	if err1 != nil {
		log.Println(err1)
		c.JSON(200, resError(3, "连接错误！"))
		return
	}
	err = dbm.CreateTable(body.TableName, body.Fields, body.TableArgs)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(4, "创建表错误！"+err.Error()))
	}
	c.JSON(200, success(gin.H{
		"tableName": body.TableName,
	}))
}

// 修改表名
func RenameTableName(c *gin.Context) {
	var body struct {
		DsId         uint   `json:"dsId"`
		DatabaseName string `json:"databaseName"`
		OldTableName string `json:"oldTableName"`
		NewTableName string `json:"newTableName"`
	}

	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(200, resError(1, "参数错误！"))
		return
	}

	dsRecord, err := db.GetUsersServerById(getUserId(c), body.DsId)
	if err != nil {
		c.JSON(200, resError(2, "数据源不存在！"))
		return
	}
	dbm := dialect.GetDialect(dialect.DialectMysql)
	config := serverToDatasourcesConfig(dsRecord, body.DatabaseName)

	_, err1 := dbm.ConnectDatasources(&config)
	if err1 != nil {
		c.JSON(200, resError(3, "连接目标数据库失败！"))
		return
	}

	err = dbm.RenameTable(body.OldTableName, body.NewTableName)
	if err != nil {
		c.JSON(200, resError(4, "重命名表失败！"))
		return
	}
	c.JSON(200, success(true))
}

// 删除表
func DropTable(c *gin.Context) {
	var body struct {
		DsId         uint   `json:"dsId"`
		DatabaseName string `json:"databaseName"`
		TableName    string `json:"tableName"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		log.Println(err)
		c.JSON(200, resError(1, "参数错误！"))
		return
	}

	dsRecord, err := db.GetUsersServerById(getUserId(c), body.DsId)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "数据源不存在！"))
		return
	}
	dbm := dialect.GetDialect(dialect.DialectMysql)
	config := serverToDatasourcesConfig(dsRecord, body.DatabaseName)

	_, err1 := dbm.ConnectDatasources(&config)
	if err1 != nil {
		log.Println(err1)
		c.JSON(200, resError(3, "连接目标数据库失败！"))
		return
	}
	err = dbm.DropTable(body.TableName)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(4, "删除表失败！"))
		return
	}
	c.JSON(200, success(true))

}
